function [] = imitateManufacturing (CON)
global connectorPath
global host
global port
global dbname
global user
global password

javaaddpath(CON.connectorPath); 
driver = 'com.mysql.jdbc.Driver';
url = ['jdbc:mysql://' CON.host ':' num2str(CON.port) '/' CON.dbname];
conn = database(CON.dbname, CON.user, CON.password, driver, url);

if isconnection(conn)
     planTable = cell2mat(get(fetch(exec(conn,'select ManufacturingPlan.Id, ManufacturingPlan.OrderProductTableId, Product.ComplexityOfManufacturing, Product.Id from ManufacturingPlan LEFT JOIN OrderProductTable ON ManufacturingPlan.OrderProductTableId = OrderProductTable.Id LEFT JOIN Product ON OrderProductTable.ProductId = Product.Id where ManufacturingPlan.Processed=0 ORDER BY ManufacturingPlan.Id;')),'Data'));
     overread = cell2mat(get(fetch(exec(conn, 'select OverreadFromManufPlan from ManufacturingInit;')),'Data'));
     line = cell2mat(get(fetch(exec(conn, 'select UsingLine from ManufacturingInit;')),'Data'));
     day = cell2mat(get(fetch(exec(conn, 'select CurrentDay from ManufacturingInit;')),'Data'));
     daytacts = cell2mat(get(fetch(exec(conn, 'select TactsInWorkingDay from ManufacturingInit;')),'Data'));
     readyproducts = 0;
     currplan = 1;
     changelinetacts = cell2mat(get(fetch(exec(conn, 'select TactsForChangeLine from ManufacturingInit;')),'Data'));
    
     if (~strcmp(planTable, 'No Data'))
        if (overread ~= 0)
            exec(conn,'update ManufacturingInit SET OverreadFromManufPlan = 0;');
            readyproducts = readyproducts + 1;
            daytacts = daytacts - overread;
            exec(conn,strcat('update ManufacturingPlan SET Processed = 1 where Id = ', int2str(planTable(1,1)), '; '));
            exec(conn,strcat('update OrderProductTable SET StateOrderProductId = 1 where Id = ', int2str(planTable(1,2)), '; '));
            product = planTable(1,4);
            %resources
            materials = cell2mat(get(fetch(exec(conn, strcat('select MaterialId, AmountOfMaterialForOne from ProductMaterial where ProductId = ', int2str(product),';'))),'Data'));
                for n = 1 : size(materials, 1)
                    exec(conn,strcat('insert into OrderMaterialTable(MaterialId,Count,StateOrderMaterialId) values(', num2str(materials(n,1)),', ',num2str(-materials(n,2)),',1); '));
                    exec(conn,strcat('insert into ReservedMaterial(MaterialId,Count) values(', num2str(materials(n,1)),', ',num2str(-materials(n,2)),'); '));
                end
            currplan = 2;
        end;
     
        sumtimeofchange = 0;
        plansize = size(planTable, 1);
     if (plansize > 1)
         
        state = 0; 
        while (state == 0)
            product = planTable(currplan,4);
            %change line
            if (product ~= line)
                sumtimeofchange = sumtimeofchange + changelinetacts;
                exec(conn,strcat('update ManufacturingInit SET UsingLine =', num2str(product),';'));
                daytacts = daytacts - changelinetacts;
                if (daytacts <= 0)
                    state = 1;
                    break;
                end;
            end;
            %work
                complexity = planTable(currplan,3);
                daytacts = daytacts - complexity;
                %resources
                materials = cell2mat(get(fetch(exec(conn, strcat('select MaterialId, AmountOfMaterialForOne from ProductMaterial where ProductId = ', int2str(product),';'))),'Data'));
                for n = 1 : size(materials, 1)
                    exec(conn,strcat('insert into OrderMaterialTable(MaterialId,Count,StateOrderMaterialId) values(', num2str(materials(n,1)),', ',num2str(-materials(n,2)),',1); '));
                    exec(conn,strcat('insert into ReservedMaterial(MaterialId,Count) values(', num2str(materials(n,1)),', ',num2str(-materials(n,2)),'); '));
                end
                if (daytacts > 0)        
                    exec(conn,strcat('update OrderProductTable SET StateOrderProductId = 1 where Id = ', int2str(planTable(currplan,2)), '; '));
                    exec(conn,strcat('update ManufacturingPlan SET Processed = 1 where OrderProductTableId = ', int2str(planTable(currplan,2)), '; '))
                    readyproducts = readyproducts + 1;
                else
                    state = 1;
                    exec(conn,strcat('update OrderProductTable SET StateOrderProductId = 3 where Id = ', int2str(planTable(currplan,2)), '; '));
                    if (daytacts ~= 0)
                        exec(conn,strcat('update ManufacturingInit SET OverreadFromManufPlan=', num2str(-daytacts), '; '));
                    end;  
                end
                currplan = currplan + 1;
                if (currplan > plansize)
                    state = 1;
                end;
        
        end;
        %write log
        exec(conn,strcat('insert into ManufacturingLog(Day, WorkTimeForChange,DoneProduct) values(', num2str(day), ',', num2str(sumtimeofchange), ',',  num2str(readyproducts),');'));

        
        %Build orders
        zakazTable = cell2mat(get(fetch(exec(conn,'select Order_.Id, OrderProductTable.Id, OrderProductTable.StateOrderProductId, Order_.OrderStateId from OrderProductTable LEFT JOIN Order_ ON OrderProductTable.OrderId = Order_.Id where Order_.OrderStateId = 1 ORDER BY Order_.Id;')),'Data'));
        
         if ( size(zakazTable,1) > 0)
          %zakaz = zakazTable(1,1);
          for i=1:size(zakazTable, 2)
             %j = i + 1;
             j = i;
             zakaz = zakazTable(j,1);
             orderReady = true;
             while (zakaz == zakazTable(j,1))
                 if (zakazTable(j,3) ~= 1)
                     orderReady = false;
                 end
                 j = j + 1;
             end
             if (orderReady)
                 exec(conn,strcat('update Order_ SET OrderStateId = 2 where Id = ', int2str(zakaz), '; '));
             end
             i = j;
          end
          
         end
         
     end; %if (size(planTable, 1) > 1)
     end; %(~strcmp(planTable, 'No Data'))
    
end
end